const app = getApp()
import {
  parseTime
} from '../../common/js/utils'
Component({
  data: {
    background: [],
    currentIndex: 0,
    indicatorDots: false,
    vertical: false,
    autoplay: true,
    circular: true,
    interval: 2000,
    duration: 500,
    navList: [
      // // {
      // //   id:1,
      // //   name:'首页',
      // //   icon:'/static/iconfont/a2_01.png',
      // //   url:'/pages/index/index'
      // // },
      // {
      //   id:2,
      //   name:'产品中心',
      //   icon:'/static/iconfont/a2_02.png',
      //   url:"/pages/list/list?type=1"
      // },
      // {
      //   id:3,
      //   name:'新闻中心',
      //   icon:'/static/iconfont/a2_03.png',
      //   url:"/pages/list/list?type=2"
      // },
      // {
      //   id:4,
      //   name:'合作案例',
      //   icon:'/static/iconfont/a2_04.png',
      //   url:"/pages/list/list?type=3"
      // },
      // // {
      // //   id:5,
      // //   name:'售后服务',
      // //   icon:'/static/iconfont/a2_05.png',
      // // },
      // // {
      // //   id:6,
      // //   name:'荣誉资质',
      // //   icon:'/static/iconfont/a2_06.png',
      // // },
      // {
      //   id:7,
      //   name:'在线留言',
      //   icon:'/static/iconfont/a2_07.png',
      //   url:'/pages/note/note'
      // },
      // {
      //   id:8,
      //   name:'关于我们',
      //   icon:'/static/iconfont/a2_08.png',
      //   url:'/pages/anoutmy/anoutmy'
      // }
    ],
    initList: [],
    cardList: [
    ],
    journalismList: [
    ]
  },
  ready() {
    // tt.checkFollowAwemeState({
    //   awemeId:'Moxiaohong',
    //   complete:res=>{
    //     console.log(res,'查看');
    //   },

    // })
    tt.setNavigationBarTitle({
      title: '首页'
    });

    this.getBanner()
    this.getList()

  },
  properties: {

  },
  attached() {
    tt.showLoading({
      title: 'loading',
    });
    setTimeout(()=>{
      this.getContent()
    },200)
  },
  methods: {
    getList() {
      let that = this
      app.getRequest('/applet/nav/list', 'GET', '').then(res => {
        if (res.code == 200) {
          res.data.bannerList.map(item=>{
            item.url = app.globalData.serverUrl+item.url
          })
          that.setData({
            navList: res.data.bannerList.filter(item => {
              return (item.isView == 1)
            })
          })
        }
      })
    },
    getBanner() {
      let that = this
      app.getRequest('/applet/banner/list', 'GET', '').then(res => {
        if (res.code == 200) {
          res.data.bannerList.map(item=>{
            item.url = app.globalData.serverUrl+item.url
          })
          that.setData({
            background: res.data.bannerList
          })
        }
      })
    },
    getContent() {
      let that = this
      let endArr = []
      if (that.data.navList.length > 0) {
        let newList = that.data.navList.filter(item => {
          return (item.type != 4 && item.type != 5)
        })
        newList.map(v => {
          let newArr = {};
          newArr.title = v.name.slice(0, 2)
          newArr.type = v.type
          newArr.fid = v.id
          newArr.child = []
          endArr.push(newArr)
        })
        app.getRequest('/applet/content/home/page2', 'GET', '').then(res => {
          if (res.code == 200) {
            res.data.homePage.map(item => {
              item.createTime = parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
              item.coverUrl = app.globalData.serverUrl+item.coverUrl
              endArr.map(vitem => {
                if (vitem.fid == item.navId) {
                  vitem.child.push(item)
                }
              })
            })
            endArr = endArr.filter(item => {
              return item.child.length != 0
            })
            that.setData({
              initList: endArr
            })
            tt.hideLoading();
          }
        })
      } else {
        setTimeout(() => {
          let newList = that.data.navList.filter(item => {
            return (item.type != 4 && item.type != 5)
          })
          newList.map(v => {
            let newArr = {};
            newArr.title = v.name.slice(0, 2)
            newArr.type = v.type
            newArr.fid = v.id
            newArr.child = []
            endArr.push(newArr)
          })
          app.getRequest('/applet/content/home/page2', 'GET', '').then(res => {
            if (res.code == 200) {
              res.data.homePage.map(item => {
                item.createTime = parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
                endArr.map(vitem => {
                  if (vitem.fid == item.navId) {
                    vitem.child.push(item)
                  }
                })
              })
              endArr = endArr.filter(item => {
                return item.child.length != 0
              })
              that.setData({
                initList: endArr
              })
              tt.hideLoading();
            }
          })
        }, 500)
      }

      // let that = this
      // let endArr = []
      // that.setData({
      //   initList: []
      // })
      // if (that.data.navList.length > 0) {
      // let newList = that.data.navList.filter(item => {
      //   return (item.type != 4 && item.type != 5)
      // })
      // newList.map(item => {
      //   endArr.push(this.getContentDetail(item.id))
      // })
      // setTimeout(() => {
      //   let newArrA = endArr.filter(item => Object.keys(item).length !== 0)
      //   this.setData({
      //     initList: newArrA
      //   })
      //   tt.hideLoading();
      // }, 300)
      // } else {
      // setTimeout(() => {
      // let newList = that.data.navList.filter(item => {
      //   return (item.type != 4 && item.type != 5)
      // })
      // newList.map(item => {
      //   endArr.push(this.getContentDetail(item.id))
      // })
      // setTimeout(() => {
      //   let newArrA = endArr.filter(item => Object.keys(item).length !== 0)
      //   this.setData({
      //     initList: newArrA
      //   })
      //   tt.hideLoading();
      // }, 200)
      // }, 300)
      // }
    },
    getContentDetail() {
      let that = this
      let endArr = []
      let newList = that.data.navList.filter(item => {
        return (item.type != 4 && item.type != 5)
      })
      newList.map(v => {
        let newArr = {};
        newArr.title = v.name.slice(0, 2)
        newArr.type = v.type
        newArr.fid = v.id
        newArr.child = []
        endArr.push(newArr)
      })
      app.getRequest('/applet/content/home/page2', 'GET', '').then(res => {
        if (res.code == 200) {
          res.data.homePage.map(item => {
            item.createTime = parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
            endArr.map(vitem => {
              if (vitem.fid == item.navId) {
                vitem.child.push(item)
              }
            })
          })

        }
      })
      return endArr
      // app.getRequest('/applet/content/page', 'GET', data).then(res => {
      //   if (res.code == 200) {
      //     if (res.data.contentPage.total != 0) {
      //       res.data.contentPage.records.map(item => {
      //         item.createTime = parseTime(item.createTime, '{y}-{m}-{d} {h}:{i}:{s}')
      //       })
      //       that.data.navList.map(item => {
      //         if (item.id === res.data.contentPage.records[0].navId) {
      //           newArr.title = item.name.slice(0, 2)
      //           newArr.type = item.type
      //           newArr.fid = id
      //         }
      //       })
      //       newArr.child = res.data.contentPage.records
      //     }
      //   }
      // })
      // return newArr
    },
    animationFinish(e) {
      this.setData({
        currentIndex: e.detail.current
      })
    },
    cardHeld(e) {
      tt.navigateTo({
        url: "/pages/details/details?type=" + e.currentTarget.dataset.type + "&id=" + e.currentTarget.dataset.id,
      });
    },
    // journalismHeld(e) {
    //   tt.navigateTo({
    //     url: "/pages/details/details?type="+e.currentTarget.dataset.type+"&id=" + e.currentTarget.dataset.id,
    //   });
    // },
    toList(e) {
      tt.navigateTo({
        url: "/pages/list/list?type=" + e.currentTarget.dataset.type + '&id=' + e.currentTarget.dataset.fid + '&typenew=' + e.currentTarget.dataset.type,
      });
    },
    ToNavigator(e) {
      let that = this
      if (e.currentTarget.dataset.id == 1) {
        tt.reLaunch({
          url: e.currentTarget.dataset.url,
          success: (res) => {
            that.setData({
              isShow: !that.data.isShow
            })
          },
        });
      } else {
        tt.navigateTo({
          url: e.currentTarget.dataset.url + '&id=' + e.currentTarget.dataset.id + '&typenew=' + e.currentTarget.dataset.type,
          success: (res) => {
            that.setData({
              isShow: !that.data.isShow
            })
          },
          fail: err => {
            console.log(err);
          }
        });

        // if(e.currentTarget.dataset.type<4){
        //   tt.navigateTo({
        //     url: e.currentTarget.dataset.url+'&id='+e.currentTarget.dataset.id,
        //     success: (res) => {
        //       that.setData({
        //         isShow: !that.data.isShow
        //       })
        //     },
        //     fail:err=>{
        //       console.log(err);
        //     }
        //   });
        // }else if(e.currentTarget.dataset.type==5){
        //   tt.navigateTo({
        //     url: e.currentTarget.dataset.url+'?id='+e.currentTarget.dataset.id,
        //     success: (res) => {
        //       that.setData({
        //         isShow: !that.data.isShow
        //       })
        //     },
        //     fail:err=>{
        //       console.log(err);
        //     }
        //   });
        // }
        // else{
        //   tt.navigateTo({
        //     url: e.currentTarget.dataset.url,
        //     success: (res) => {
        //       that.setData({
        //         isShow: !that.data.isShow
        //       })
        //     },
        //   });
        // }

      }

    }
  }
})